package com.qf.zyh.web.controller;

import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Users;
import com.qf.zyh.services.UserSer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet{
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String date = req.getParameter("date");
        Users us = new Users(username,password,email,sex,sdf.parse(date),new Date());
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserSer uss = (UserSer) ac.getBean("us");
        System.out.println(us+"new的对象");
        int insuser = uss.insuser(us);
        System.out.println("返回值"+insuser);
        if (insuser>0) {
            return Const.SysConst.REDIRECT+"login.jsp";
        }
        return Const.SysConst.FORWARD+"register.jsp";
    }
    public String login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UserSer us = (UserSer) ac.getBean("us");
        System.out.println(us+"us");
        System.out.println(username+"usna");
        Users getname = us.getname(username, password);
        System.out.println(getname);
        if (getname!=null) {
            return Const.SysConst.REDIRECT+"info?method=getadd";
        }
        return Const.SysConst.REDIRECT+"login.jsp";
    }
}
